<?php
	include_once '../../public/inc/DB.php';
	$db = new DB('jusmor_news');
	
	$action = $_POST['action'];
	
	if($action == 'save') {
		if(! empty($_POST)) {
			$news_title = $_POST['news_title'];
			$keywords = $_POST['keywords'];
			$description = $_POST['description'];
			$news_content = trim($_POST['news_content']);
			$news_cate_id = $_POST['news_cate_id'];
			$create_time = $_POST['s_date'];
			if(empty($news_title) || empty($news_content) || empty($news_cate_id)) {
				echo '相关信息未填写完整！3秒后跳回上一页……<script>setTimeout(function () {window.location.href="../news/news_list.php"; }, 3000);</script>';
			}
			$data = array('news_title'=>$news_title, 'keywords'=>$keywords, 'description'=>$description, 'news_content'=>$news_content, 'news_cate_id'=>$news_cate_id, 'create_time'=>$create_time, 'update_time'=>$create_time);
			
			$res = $db->insert($data);
			if($res) {
				echo '保存成功！3秒后跳转到文章列表……<script>setTimeout(function () {window.location.href="../news/news_list.php"; }, 3000);</script>';
			} else {
				echo '保存失败！';
			}
		}
		exit();
	}
	
	if($action == 'save_edit') {
		if(! empty($_POST)) {
			$news_id = $_POST['news_id'];
			$news_title = $_POST['news_title'];
			$keywords = $_POST['keywords'];
			$description = $_POST['description'];
			$news_content = trim($_POST['news_content']);
			$news_cate_id = $_POST['news_cate_id'];
			$update_time = $_POST['s_date'];
				
			$data = array('news_title'=>$news_title, 'keywords'=>$keywords, 'description'=>$description, 'news_content'=>$news_content, 'news_cate_id'=>$news_cate_id, 'update_time'=>$update_time);
			$condition = array('news_id'=>$news_id);
	
			$res = $db->update($data, $condition);
			if($res) {
				echo '修改成功！3秒后跳转到文章列表……<script>setTimeout(function () {window.location.href="../news/news_list.php"; }, 3000);</script>';
				
			} else {
				echo '修改失败！';
			}
		}
		exit();
	}
	
	